package com.lc.luzhengtang.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.conn.ConnChangeAddress;
import com.lc.luzhengtang.conn.ConnDelAddress;
import com.lc.luzhengtang.conn.ConnFeedBack_BeiFen;
import com.lc.luzhengtang.util.LovelyToast;
import com.lc.luzhengtang.view.ShowCityPicker;
import com.lljjcoder.citypickerview.widget.CityPicker;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

import org.yapp.utils.permission.PermissionUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.lc.luzhengtang.R.id.editText_name;

/**
 * Created by Administrator on 2017/5/26.
 */
public class AddressChangeActivity extends BaseActivity {

    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_submit)
    TextView textSubmit;
    @BindView(editText_name)
    EditText editTextName;
    @BindView(R.id.edit_telephone)
    EditText editTelephone;
    @BindView(R.id.text_provice)
    TextView textProvice;
    @BindView(R.id.layouy_dingwei)
    LinearLayout layouyDingwei;
    @BindView(R.id.editText_shouhuodetail)
    EditText editTextShouhuodetail;
    @BindView(R.id.editText_shouhuoperson)
    EditText editTextShouhuoperson;
    @BindView(R.id.img_address_add)
    CheckBox imgAddressAdd;
    @BindView(R.id.text_delete)
    TextView textDelete;
    //三级联动省市区
    private String province;
    private String city;
    private String district;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addaddress_change);
        ButterKnife.bind(this);
        PermissionUtil

                .with(AddressChangeActivity.this)

                .permissions(

                        PermissionUtil.PERMISSIONS_GROUP_LOACATION//定位授权

//                        PermissionUtil.PERMISSIONS_GROUP_CONTCATS//获取联系人授权

                ).request();
//        editText_name
editTextName.setText(getIntent().getStringExtra("shouhuoname"));
        editTelephone.setText(getIntent().getStringExtra("phone"));
        textProvice.setText(getIntent().getStringExtra("city"));
        editTextShouhuodetail.setText(getIntent().getStringExtra("detail"));

    }

    @OnClick({R.id.img_news, R.id.text_submit, editText_name, R.id.edit_telephone, R.id.text_provice, R.id.layouy_dingwei, R.id.editText_shouhuodetail, R.id.editText_shouhuoperson, R.id.img_address_add, R.id.text_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.text_submit:
                if (TextUtils.isEmpty(editTextName.getText().toString())) {
                    LovelyToast.makeText(AddressChangeActivity.this, "收货人不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editTelephone.getText().toString())) {
                    LovelyToast.makeText(AddressChangeActivity.this, "手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editTelephone.getText().toString())) {
                    LovelyToast.makeText(AddressChangeActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editTextShouhuodetail.getText().toString())) {
                    LovelyToast.makeText(AddressChangeActivity.this, "收货地址不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
                    sendData();
                }


                //   finish();
                break;
            case editText_name:
                break;
            case R.id.edit_telephone:
                break;
            case R.id.text_provice:
                break;
            case R.id.layouy_dingwei:

                final CityPicker cityPicker = new ShowCityPicker().show(AddressChangeActivity.this);
                //监听方法，获取选择结果
                cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
                    @Override
                    public void onSelected(String... citySelected) {

                        province = citySelected[0];

                        city = citySelected[1];

                        district = citySelected[2];

                        textProvice.setText(new StringBuilder().append(province).append("-").append(city).append("-").append(district).toString());
                    }

                    @Override
                    public void onCancel() {
                        cityPicker.hide();
                    }
                });
                break;
            case R.id.editText_shouhuodetail:
                break;
            case R.id.editText_shouhuoperson:
                break;
            case R.id.img_address_add:

                //imgAddressAdd.setSelected(true);

                if (imgAddressAdd.isChecked()) {
                    // Toast.makeText(context, "呵呵", Toast.LENGTH_SHORT).show();
                    setDefaultAddres();

                } else {
                    Toast.makeText(context, "干他妈的", Toast.LENGTH_SHORT).show();
                }

                break;

            case R.id.text_delete:
                //   Toast.makeText(context, "删除了地址", Toast.LENGTH_SHORT).show();
                getDelAddress();
                break;

        }
    }

//    ConnAddAddress


    public void sendData() {
        Log.d(getIntent().getStringExtra("id"), "aaaaaaaaaaa");
        Log.d(getIntent().getStringExtra("id"), "aaaaaaaaaaa");

        new ConnChangeAddress(getIntent().getStringExtra("id"), BaseApplication.basePreferences.readUid(), editTextName.getText().toString(), editTelephone.getText().toString(),textProvice.getText().toString(), editTextShouhuodetail.getText().toString(), "0", new AsyCallBack<CommenBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
                LovelyToast.makeText(AddressChangeActivity.this, commenBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);

            }
        }


        ).execute(AddressChangeActivity.this, false);
    }


    public void getDelAddress() {


        new ConnDelAddress(BaseApplication.basePreferences.readUid(), getIntent().getStringExtra("id"), new AsyCallBack<CommenBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
                if (commenBean.getCode() == 200) {
                    LovelyToast.makeText(AddressChangeActivity.this, commenBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }

                finish();
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }


        ).execute(AddressChangeActivity.this, false);


    }


    public void setDefaultAddres() {


        new ConnFeedBack_BeiFen(BaseApplication.basePreferences.readUid(), 1, getIntent().getStringExtra("id"), new AsyCallBack<CommenBean>() {
            @Override
            public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
                LovelyToast.makeText(AddressChangeActivity.this, commenBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                finish();

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                LovelyToast.makeText(AddressChangeActivity.this, "网络连接异常", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }


        ).execute(AddressChangeActivity.this, false);

    }


}
